﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

namespace TracyConsoleVeryCDTest
{
    /// <summary>
    /// This class maintains DOM for VeryCD RSS document
    /// </summary>
    public class VeryCDRSS
    {
        /// <summary>
        /// RSS DOM object
        /// </summary>
        public rss RSS { get; private set; }

        /// <summary>
        /// Contains all resources from the RSS feed. This object will be initialized by TransformRSS method.
        /// </summary>
        public target Target { get; private set; }

        private string[] ResourceFormats;
        private string[] ResourceNames;
        private string[] ResourceResolutions;
        private string[] ResourceSubtitles;

        public VeryCDRSS()
        {
            //Load configurations
            ResourceFormats = TracySettings.Default.ResourceFormat.Split(',');
            ResourceNames = TracySettings.Default.ResourceName.Split(',');
            ResourceResolutions = TracySettings.Default.ResourceResolution.Split(',');
            ResourceSubtitles = TracySettings.Default.ResourceSubtitle.Split(',');
        }

        /// <summary>
        /// Load xml from the hard drive and initialize RSS object using the xml document
        /// </summary>
        /// <param name="path"></param>
        public void LoadRSS(string path)
        {
            //Read xml and deserialize to RSS object
            FileStream fs = new FileStream(path, FileMode.Open);
            XmlReader reader = XmlReader.Create(fs);
            XmlSerializer serializer = new XmlSerializer(typeof(rss));
            RSS = (rss)serializer.Deserialize(reader);
            Target = new target();
        }

        /// <summary>
        /// Tranform RSS content.
        /// The title section of the RSS feed will be splitted and matched with pre-defined keywords and finally stored into the target DOM object.
        /// </summary>
        public void TransformRSS()
        {
            List<targetResource> resources = new List<targetResource>();

            //loop through all rss items
            foreach (rssChannelItem item in RSS.channel.item)
            {
                targetResource res = new targetResource();

                //Split out segments from rss item title
                string[] segments = item.title.Split(
                    TracySettings.Default.SegmentSeparators.Split(','),
                    StringSplitOptions.RemoveEmptyEntries);

                //Match pre-defined keywords with all segments in the rss content
                foreach (string segment in segments)
                {
                    #region Resource name
                    foreach (string name in ResourceNames)
                    {
                        if (segment.Contains(name))
                        {
                            res.name = segment;
                            break;
                        }
                    }
                    #endregion

                    #region Resource format
                    foreach (string format in ResourceFormats)
                    {
                        if (segment.Contains(format))
                        {
                            res.format = segment;
                            break;
                        }
                    }
                    #endregion

                    #region Resource subtitle
                    foreach (string subtitle in ResourceSubtitles)
                    {
                        if (segment.Contains(subtitle))
                        {
                            res.subtitle = segment;
                            break;
                        }
                    }
                    #endregion

                    #region Resource resolution
                    foreach (string resolution in ResourceResolutions)
                    {
                        if (segment.Contains(resolution))
                        {
                            res.resolution = segment;
                            break;
                        }
                    }
                    #endregion
                }

                //Add this resource to the result list
                resources.Add(res);
            }

            Target.resource = resources.ToArray();
        }

        /// <summary>
        /// Serialize and save the target DOM object to an xml document.
        /// </summary>
        /// <param name="path"></param>
        public void SaveResources(string path)
        {
            //Serialize Target object and save to specific xml
            FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
            XmlWriter writer = XmlWriter.Create(fs);
            XmlSerializer serializer = new XmlSerializer(typeof(target));
            serializer.Serialize(writer, Target);
        }
    }
}
